package com.helloworld.demo.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest2 {
    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) {

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println("子线程启动");
                try{
                    lock.lockInterruptibly();

                    try{
                        System.out.println("子线程获取了锁");
                    }finally {
                        lock.unlock();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("子线程等锁的过程被中断");
                }


            }
        }, "Thread-test");


        lock.lock();
        try{
            System.out.println("main 获取了锁");

            thread.start();

            Thread.sleep(1000);

            thread.interrupt();

            System.out.println("执行了子线程的中断");

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

}
